package com.string.actortv.util;

import com.string.actortv.dto.MovieInfo;
import com.string.actortv.dto.MovieList;
import com.string.actortv.dto.MovieTypeList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import static com.string.actortv.util.CacheHelper.CACHE;

public class RenrenUtil {

    private static final String BASE_URL = "https://www.rrzyw.cc";
    //https://www.rrzyw.cc/index.php/vod/search/page/1.html?wd=
    private static final String BASE_PAGE_URL = "https://www.rrzyw.cc/index.php/vod/search/page/";
    private static final String BASE_PLAY_URL = "https://www.rrzyw.cc/rrm3u8/index.php?url=";
    private static String HTML;
    private static String movieHtml;
    private static Document HTML_DOCUMENT;

    public static void getMovieInfoAsync(String href, String name, MovieInfoCallBack movieInfoCallBack) {
        //获取电影集数列表
        if (CACHE.containsKey(href)
                && CACHE.get(href) != null) {
            movieHtml = CACHE.get(href).toString();

            getInfo(name, movieInfoCallBack);
        } else {
            MyHttpUtil.getAsync(href, movieHtml -> {
                if ("".equals(movieHtml)) {
                    movieInfoCallBack.execute(null);
                    return;
                }
                CACHE.put(href, movieHtml);

                getInfo(name, movieInfoCallBack);
            }, null);
        }
    }

    private static void getInfo(String name, MovieInfoCallBack movieInfoCallBack) {
        Document movieHtmlDocument = Jsoup.parse(movieHtml);
        String imgUrl = movieHtmlDocument.getElementsByClass("img-responsive")
                .get(0).attr("src");
        String desc = movieHtmlDocument.getElementsByClass("stui-content__desc").get(0).text();
        Elements listElements = movieHtmlDocument
                .getElementsByClass("stui-content__playlist");
        List<MovieInfo.SeasonList> seasonList = new ArrayList<>();
        if (listElements != null && !listElements.isEmpty()) {
            Elements seasonElements = listElements.get(0).getElementsByTag("li");
            for (Element seasonElement : seasonElements) {
                String str = seasonElement.getElementsByTag("input").get(0).val();
                String seasonName = str.split("\\$")[0];
                String url = BASE_PLAY_URL + str.split("\\$")[1];
                //去除多余内容
                int otherIndex = url.indexOf("线路2");
                if (otherIndex > -1) {
                    url = url.substring(0, otherIndex);
                }

                seasonList.add(new MovieInfo.SeasonList(seasonName, url));
            }
        }
        movieInfoCallBack.execute(new MovieInfo(name, imgUrl, desc, seasonList));
    }

    public interface MovieInfoCallBack {
        void execute(MovieInfo movieInfo);
    }

    public static MovieInfo getMovieInfo(String href) {
        //获取电影集数列表
        if (CACHE.containsKey(href)
                && CACHE.get(href) != null) {
            movieHtml = CACHE.get(href).toString();
        } else {
            movieHtml = MyHttpUtil.get(href);
            if ("".equals(movieHtml)) {
                return null;
            }
            CACHE.put(href, movieHtml);
        }

        Document movieHtmlDocument = Jsoup.parse(movieHtml);
        String imgUrl = movieHtmlDocument.getElementsByClass("img-responsive")
                .get(0).attr("src");
        Elements descElements = movieHtmlDocument.getElementsByClass("stui-content__desc");
        String desc = "";
        if (!descElements.isEmpty()) {
            desc = descElements.get(0).text();
        }
        Elements listElements = movieHtmlDocument
                .getElementsByClass("stui-content__playlist");
        List<MovieInfo.SeasonList> seasonList = new ArrayList<>();
        if (listElements != null && !listElements.isEmpty()) {
            Elements seasonElements = listElements.get(0).getElementsByTag("li");
            for (Element seasonElement : seasonElements) {
                String str = seasonElement.getElementsByTag("input").get(0).val();
                String name = str.split("\\$")[0];
                String url = BASE_PLAY_URL + str.split("\\$")[1];
                //去除多余内容
                int otherIndex = url.indexOf("线路2");
//                Log.d("otherIndex", "" + otherIndex);
                if (otherIndex > -1) {
                    url = url.substring(0, otherIndex);
                }

                seasonList.add(new MovieInfo.SeasonList(name, url));
            }
        }
        return new MovieInfo("", imgUrl, desc, seasonList);
    }

    public interface MovieTypeCallBack {
        void execute(List<MovieTypeList> movieTypeList);
    }

    public static void getMovieTypeAsync(MovieTypeCallBack callBack) {
        //获取电影分类列表
        if (CACHE.containsKey(BASE_URL)
                && (HTML = CACHE.get(BASE_URL).toString()) != null) {
            HTML_DOCUMENT = Jsoup.parse(HTML);
            getType(callBack);
        } else {
            MyHttpUtil.getAsync(BASE_URL, result -> {
                if ("".equals(result)) {
                    callBack.execute(new ArrayList<>());
                    return;
                }
                HTML = result;
                CACHE.put(BASE_URL, HTML);
                HTML_DOCUMENT = Jsoup.parse(HTML);
                getType(callBack);
            }, null);
        }

    }

    private static void getType(MovieTypeCallBack callBack) {
        //获取电影类型列表
        Elements typeElements = HTML_DOCUMENT
                .getElementsByClass("stui-header__menu")
                .get(0).getElementsByTag("li");
        List<MovieTypeList> typeList = new ArrayList<>();
        for (Element typeElement : typeElements) {
            String name = typeElement.getElementsByTag("a").get(0).text();
            String oldUrl = typeElement.getElementsByTag("a").get(0).attr("href");
            String url = BASE_URL + oldUrl.substring(0, oldUrl.contains(".html") ? oldUrl.indexOf(".html") : oldUrl.length()) + "/page/";
            if ("/".equals(typeElement.getElementsByTag("a").get(0).attr("href"))) {
                name = "全部";
                url = BASE_PAGE_URL;
            }
            typeList.add(new MovieTypeList(name, url));
        }
        callBack.execute(typeList);
    }

    public interface MovieListCallBack {
        void execute(List<MovieList> movieList);
    }

    public static void getMovieListAsync(int pageNo, String searchStr, String url, MovieListCallBack movieListCallBack) {
        String pageUrl = url + pageNo + "?wd=" + searchStr;
        if (CACHE.containsKey(pageUrl)
                && (HTML = CACHE.get(pageUrl).toString()) != null) {
            getList(HTML, movieListCallBack);
        } else {
            MyHttpUtil.getAsync(pageUrl, result -> {
                HTML = result;
                if ("".equals(HTML)) {
                    movieListCallBack.execute(null);
                    return;
                }
                CACHE.put(pageUrl, HTML);

                getList(HTML, movieListCallBack);
            }, null);
        }
    }

    private static void getList(String html, MovieListCallBack movieListCallBack) {
        HTML_DOCUMENT = Jsoup.parse(html);
        //获取当前电影列表
        Elements movieElements = HTML_DOCUMENT
                .getElementsByClass("title");
        List<MovieList> movieList = new ArrayList<>();
        if (movieElements != null) {
            for (Element movieElement : movieElements) {
                Elements elements = movieElement.getElementsByTag("a");
                if (!elements.isEmpty()) {
                    String name = elements.get(0).text();
                    String url = BASE_URL + elements.get(0).attr("href");
                    movieList.add(new MovieList(name, url));
                }
            }
        }
        movieListCallBack.execute(movieList);
    }

    public interface MovieUrlCallBack {
        void execute(String playUrl);
    }

    public static void getMovieUrlAsync(String pageUrl, MovieUrlCallBack movieUrlCallBack) {
        MyHttpUtil.getAsync(pageUrl, result -> {
            int beginIndex = result.indexOf("url: '");
            int endIndex = result.indexOf("'", beginIndex + 6);

            movieUrlCallBack.execute(result.substring(beginIndex + 6, endIndex));
        }, null);

    }

}
